package com.yhy.wxzn.wcs.device.plc.xj;

import com.yhy.wxzn.common.utils.SpringUtil;
import com.yhy.wxzn.common.utils.WcsUtils;
import com.yhy.wxzn.wcs.device.plc.bean.FourCarPlc;
import com.yhy.wxzn.wcs.entity.WcsDeviceInfo;
import com.yhy.wxzn.wcs.entity.WcsFourWayTask;
import com.yhy.wxzn.wcs.service.IWcsDeviceLogService;
import com.yhy.wxzn.wcs.service.IWcsFourWayTaskService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Auther: 97354
 * @Date: 2023/11/11 14:55
 * @Description: SxcProtocol
 * @Version 1.0.0
 */
@Component
@Scope("prototype")
@Slf4j
public class XjSxcProtocol extends Thread {
    private volatile WcsDeviceInfo sxCar;
    private volatile String plcName;
    public volatile XjPlcDriver xjPlcDriver;
    private IWcsFourWayTaskService fourWayTaskService;
    private IWcsDeviceLogService deviceLogService;

    public XjSxcProtocol(String plcName, WcsDeviceInfo sxCar) {
        this.plcName = plcName;
        this.sxCar = sxCar;
    }

    private void init() {
        fourWayTaskService = SpringUtil.getBean(IWcsFourWayTaskService.class);
        deviceLogService = SpringUtil.getBean(IWcsDeviceLogService.class);
    }

    @Override
    @SneakyThrows
    public void run() {
        //等待Spring 容器加载完成
        while (true) {
            try {
                if (SpringUtil.getApplicationContext() != null) {
                    init();
                    break;
                }
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while (true) {
            try {
                if (!xjPlcDriver.xjHelper.isConnectd()) {
                    sleep(10000);
                    continue;
                }
                fourWayTaskProcess();
                WcsUtils.sleep(100);
            } catch (Exception e) {
                log.error("四向车任务处理异常", e);
            }
        }
    }

    private void fourWayTaskProcess() {
        try {
            if (!CollectionUtils.isEmpty(xjPlcDriver.getDriverData().getFourCarPlcMap())) {
                xjPlcDriver.getDriverData().getFourCarPlcMap().entrySet().parallelStream().forEach(fourCarPlcEntry -> {
                            FourCarPlc carPlc = fourCarPlcEntry.getValue();
                            //查询小车是否有电并
                            if (carPlc.getDcdl() < 10) {
                                //发送充电命令
                                return;
                            }
                            //空闲且小车在线
                            if (carPlc.getCarStatus() != 1 || carPlc.getHeartBeat() != 1) {
                                return;
                            }
                            //查找四向车任务进行下发
                            List<WcsFourWayTask> taskList = fourWayTaskService.getUnfinishedByFourWayList();
                            if (CollectionUtils.isEmpty(taskList)) {
                                return;
                            }
                            //处理任务
                            dealBoundTask(carPlc, taskList);
                        }
                );
            }
        } catch (Exception ex) {
            log.error("hoistProcess 异常", ex);
        }
    }

    private void dealBoundTask(FourCarPlc carPlc, List<WcsFourWayTask> taskList) {
        WcsFourWayTask currentTask = taskList.get(0);
        /**
         *1.1：获取任务终点确定到哪个提升机
         *1.2：获取提升机当前层，确定四向车当前坐标
         * */
        //todo
    }

}
